home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacGraphicsUtils.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  263 lines

  1. /*
  2.  * @(#)MacGraphicsUtils.java    1.6 98/01/30
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21.  
  22. package com.sun.java.swing.plaf.mac;
  23.  
  24. import com.sun.java.swing.*;
  25. import java.awt.Color;
  26. import java.awt.Dimension;
  27. import java.awt.Graphics;
  28. import java.awt.Font;
  29. import java.awt.FontMetrics;
  30. import java.awt.Rectangle;
  31. import java.awt.Component;
  32. import java.awt.Insets;
  33.  
  34. import com.sun.java.swing.plaf.basic.*;
  35.  
  36. /**
  37.  * @version @(#)MacGraphicsUtils.java    1.0 11/24/97
  38.  * @author Symantec
  39.  */
  40.  
  41. public class MacGraphicsUtils implements SwingConstants 
  42. {
  43.     public static final Font controlFont = new Font("Dialog", Font.PLAIN, 12);
  44.  
  45.     public static Color control = new Color(192,192,192);
  46.     public static Color controlLight = new Color(231, 231, 231);
  47.     public static Color controlShadow = new Color(102,102,102);
  48.     public static Color controlLightShadow = new Color(155,155,155);
  49.     public static Color controlHighlight = Color.white;
  50.     public static Color controlDisabled = new Color(117, 117, 117);
  51.     public static Color controlBlack = Color.black;
  52.     public static Color controlWhite = Color.white;
  53.     
  54.     public final static Color GS3Color = UIManager.getColor("GrayscaleAppearance3");
  55.  
  56.     /**
  57.      * Convenience method for drawing a button bezel.
  58.      */
  59.     static void drawButtonBezel(Graphics g, int x, int y,
  60.                 int width, int height, boolean raised)
  61.     {
  62.     // ** Paint the button in
  63.     if(raised) {
  64.         g.setColor(MacGraphicsUtils.controlWhite);
  65.         g.drawLine(x+1, y+1, x+1, y+1);
  66.         
  67.         g.setColor(MacGraphicsUtils.controlLight);
  68.         g.drawLine(x, y, x+width-3, y);
  69.         g.drawLine(x, y, x, y+height-4);
  70.         
  71.         g.setColor(controlLightShadow);
  72.         g.drawLine(x+1, y+height-2, x+width-3, y+height-2);
  73.         g.drawLine(x+width-2, y+height-1, x+width-2, y+1);
  74.         
  75.         g.drawLine(x+width-3, y+height-3, x+width-3, y+height-3);
  76.         g.drawLine(x+width-1, y+0, x+width-1, y+0);
  77.         g.drawLine(x+0, y+height-1, x+0, y+height-1);
  78.         
  79.         g.setColor(MacGraphicsUtils.controlShadow);
  80.         g.drawLine(x+1, y+height-1, x+width-1, y+height-1);
  81.         g.drawLine(x+width-1, y+height-2, x+width-1, y+1);
  82.         g.drawLine(x+width-2, y+height-2, x+width-2, y+height-2);
  83.     } else {
  84.         g.setColor(controlWhite);
  85.         g.drawLine(x + width-2, y + height-2, x + width-2, y + height-2);
  86.         
  87.         g.setColor(controlLight);
  88.         g.drawLine(x+2,       y+height-1,  x+width-1, y+height-1);
  89.         g.drawLine(x+width-1, y+height-2,  x+width-1, y+3);
  90.         
  91.         g.setColor(controlLightShadow);
  92.         g.drawLine(x+2, y+1, x+width-2, y+1);
  93.         g.drawLine(x+1, y+2, x+1,       y+height-2);
  94.         
  95.         g.drawLine(width-1, y,          x+width-1, y);
  96.         g.drawLine(x+2,     y+2,        x+2,       y+2);
  97.         g.drawLine(x,       y+height-1, x+0,       y+height-1);
  98.         
  99.         g.setColor(controlShadow);
  100.         g.drawLine(x,   y,   x+width-2, y);
  101.         g.drawLine(x,   y+1, x,         y+height-2);
  102.         g.drawLine(x+1, y+1, x+1,       y+1);
  103.     }
  104.     }
  105.     
  106.     
  107.     /**
  108.      * Draws the point (<b>x</b>, <b>y</b>) in the current color.
  109.      */
  110.     static void drawPoint(Graphics g, int x, int y) {
  111.         g.drawLine(x, y, x, y);
  112.     }
  113.  
  114.     /**
  115.      * Draws the Image tiled in the supplied rectangle.  Calls
  116.      * <b>paint()</b> for each tile.
  117.      */
  118.     static void drawTiledIcon(Component c, Icon icon, Graphics g1, int x, int y, int width, int height) {
  119.         Rectangle clipRect;
  120.         int imageWidth, imageHeight, minX, minY, maxX, maxY;
  121.  
  122.         Graphics g = g1.create();
  123.  
  124.         clipRect = g1.getClipBounds();
  125.  
  126.         imageWidth = icon.getIconWidth();
  127.         imageHeight = icon.getIconHeight();
  128.  
  129.         if (imageWidth <= 0 || imageHeight <= 0)
  130.             return;
  131.  
  132.         g.setClip(x, y, width, height);
  133.  
  134.         if (x > clipRect.x)
  135.             minX = x;
  136.         else
  137.             minX = x + imageWidth * ((clipRect.x - x) / imageWidth);
  138.  
  139.         if ((x + width) < (clipRect.x + clipRect.width))
  140.             maxX = x + width;
  141.         else
  142.             maxX = (clipRect.x + clipRect.width);
  143.  
  144.         if (y > clipRect.y)
  145.             minY = y;
  146.         else
  147.             minY= y + imageHeight * ((clipRect.y - y) / imageHeight);
  148.  
  149.         if ((y + height) < (clipRect.y + clipRect.height))
  150.             maxY = (y + height);
  151.         else
  152.             maxY = (clipRect.y + clipRect.height);
  153.  
  154.         for (x = minX; x < maxX; x += imageWidth) {
  155.             for (y = minY; y < maxY; y += imageHeight) {
  156.                 icon.paintIcon(c, g, x, y);
  157.             }
  158.         }
  159.         g.dispose();
  160.     }
  161.  
  162.     /** Draws <b>aString</b> in the rectangle defined by
  163.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>).
  164.       * <b>justification</b> specifies the text's justification, one of
  165.       * LEFT, CENTER, or RIGHT.
  166.       * If <b>background</b> is not null, a background with the specified color 
  167.       * is first drawn.
  168.       * <b>drawStringInRect()</b> does not clip to the rectangle, but instead
  169.       * uses this rectangle and the desired justification to compute the point
  170.       * at which to begin drawing the text.
  171.       * @see #drawString
  172.       */
  173.     public static void drawStringInRect(Graphics g, String aString, int x, int y,
  174.                                  int width, int height, int justification, Color background) {
  175.         FontMetrics  fontMetrics;
  176.         int          drawWidth, startX, startY, delta;
  177.  
  178.         if (g.getFont() == null) {
  179. //            throw new InconsistencyException("No font set");
  180.             return;
  181.         }
  182.         fontMetrics = g.getFontMetrics();
  183.         if (fontMetrics == null) {
  184. //            throw new InconsistencyException("No metrics for Font " + font());
  185.             return;
  186.         }
  187.  
  188.         if (justification == CENTER) {
  189.             drawWidth = fontMetrics.stringWidth(aString);
  190.             if (drawWidth > width) {
  191.                 drawWidth = width;
  192.             }
  193.             startX = x + (width - drawWidth) / 2;
  194.         } else if (justification == RIGHT) {
  195.             drawWidth = fontMetrics.stringWidth(aString);
  196.             if (drawWidth > width) {
  197.                 drawWidth = width;
  198.             }
  199.             startX = x + width - drawWidth;
  200.         } else {
  201.             startX = x;
  202.             drawWidth = width;
  203.         }
  204.  
  205.         delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
  206.         if (delta < 0) {
  207.             delta = 0;
  208.         }
  209.  
  210.         startY = y + height - delta - 2 * fontMetrics.getDescent();
  211.         if (background != null)
  212.             {
  213.             startY -= 1;
  214.             Color save = g.getColor();
  215.             g.setColor(background);
  216.             g.fillRect(startX - 4, 0, drawWidth + 8, height - 3);
  217.             g.setColor(save);
  218.             }
  219.         else
  220.             g.setClip(x, y, width, height);
  221.  
  222.         g.drawString(aString, startX, startY);
  223.     }
  224.  
  225.     public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
  226.     {
  227.         if (b.getComponentCount() > 0) {
  228.             return null;
  229.         }
  230.  
  231.         Icon icon = (Icon) b.getIcon();
  232.         String text = b.getText();
  233.  
  234.         Font font = b.getFont();
  235.         FontMetrics fm = b.getToolkit().getFontMetrics(font);
  236.       
  237.         Rectangle iconR = new Rectangle();
  238.         Rectangle textR = new Rectangle();
  239.         Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
  240.  
  241.         SwingUtilities.layoutCompoundLabel(fm, text, icon,
  242.                 b.getVerticalAlignment(), b.getHorizontalAlignment(),
  243.                 b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  244.                 viewR, iconR, textR, (text == null ? 0 : textIconGap)
  245.         );
  246.  
  247.         /* The preferred size of the button is the size of 
  248.          * the text and icon rectangles plus the buttons insets.
  249.          */
  250.  
  251.         Rectangle r = iconR.union(textR);
  252.  
  253.         Insets insets = b.getInsets();
  254.         r.width += insets.left + insets.right;
  255.         r.height += insets.top + insets.bottom;
  256.  
  257.         return r.getSize();
  258.     }
  259. }
  260.  
  261.  
  262.  
  263.